﻿using System;
using NguyenHoa.Domains;
using NguyenHoa.Services;
using NguyenHoa.Web.App_Code;

namespace NguyenHoa.Web.Controls
{
  public partial class SendCustomerOpinion : BaseControl
  {
    #region Properties
    private string m_opinionType;
    public string OpinionType
    {
      get
      {
        if (string.IsNullOrEmpty(m_opinionType))
        {
          return CustomerContactType.Opinion;
        }
        return m_opinionType;
      }

      set
      {
        m_opinionType = value;
      }
    }
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
      if (!Page.IsPostBack && !Page.IsCallback)
      {
        ccSafeCode.DataBind();
      }

    }

    protected void cbOpinion_Callback(object source, DevExpress.Web.ASPxCallback.CallbackEventArgs e)
    {
      e.Result = SendOpinion();
    }

    private string SendOpinion()
    {
      try
      {
        ccSafeCode.ValidateCaptcha(txtSafeCode.Text);
        if (!ccSafeCode.UserValidated) return Resources.Messages.ERR0037;

        #region Save Opinion
        CustomerOpinion opinion = new CustomerOpinion();
        opinion.Type = OpinionType;
        opinion.Date = DateTime.Now;
        opinion.Fullname = Utils.SafeString(txtFullName.Text);
        opinion.Email = Utils.SafeString(txtEmail.Text);
        opinion.Phone = Utils.SafeString(txtPhone.Text);
        opinion.Address = Utils.SafeString(txtAddress.Text);
        opinion.Opinion = Utils.SafeString(mmOpinion.Text);
        opinion.Title = txtTitle.Text;
        opinion.Code = VietnameseString.Convert(txtTitle.Text + DateTime.Now.ToString("dd/MM/yy-hh:mm:ss"));
        opinion.LanguageId = CurrentLanguage.Id;
        opinion.IsActive = false;
        new CustomerOpinionService().Insert(opinion);
        #endregion
      }
      catch (Exception ex)
      {
        WriteLog("SendCustomerOpinion::SendOpinion", ex);
        return Resources.Messages.ERR0036;
      }
      return string.Empty;
    }
  }
}